package com.huihuiyun.huihuiyun.server.viewobject;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

@Slf4j
public class ResultVOUtil {

    public static ResultVO createResultVO(int code, String message, Object data) {
        ResultVO resultVO = new ResultVO();
        resultVO.data = data;
        resultVO.code = code;
        resultVO.message = message;
        return resultVO;
    }

    public static ResultVO success(Object data) {
        return createResultVO(0, "成功", data);
    }

    public static ResultVO success() {
        return createResultVO(0, "成功", null);
    }

    public static ResultVO error(int code, String message) {
        return createResultVO(code, message, null);
    }

    public static ResultVO error(String message) {
        return createResultVO(1, message, null);
    }

    public static ResultVO error() {
        return createResultVO(1, "操作失败，请稍后重试。", null);
    }

    public static <T> T copyProperties(Object sourceObject, Class<T> targetObjectClass) {
        try {
            T newObject = targetObjectClass.newInstance();
            BeanUtils.copyProperties(sourceObject, newObject);
            return newObject;
        } catch (Exception e) {
            log.error("", e);
            return null;
        }
    }

    public static <T> T convert(Object sourceObject, Class<T> targetObjectClass) {
        try {
            T targetObject = targetObjectClass.newInstance();
            BeanUtils.copyProperties(sourceObject, targetObject);
            return targetObject;
        } catch (Exception e) {
            log.error("", e);
            return null;
        }
    }

    public static <T> T copyProperties(Object sourceObject, T targetObject) {
        try {
            BeanUtils.copyProperties(sourceObject, targetObject);
            return targetObject;
        } catch (Exception e) {
            log.error("", e);
            return null;
        }
    }

    public static <T> List<T> convertList(List sourceList, Class<T> targetObjectClass) {
        List<T> listTarget = new Vector<>();
        for (Object object : sourceList) {
            try {
                T targetObject = targetObjectClass.newInstance();
                BeanUtils.copyProperties(object, targetObject);
                listTarget.add(targetObject);
            } catch (Exception e) {
                log.error("", e);
            }
        }
        return listTarget;
    }

    public static <T> List<T> copyProperties(List sourceList, Class<T> targetObjectClass) {
        List<T> list1 = new Vector<>();
        for (Object object : sourceList) {
            try {
                T targetObject = targetObjectClass.newInstance();
                BeanUtils.copyProperties(object, targetObject);
                list1.add(targetObject);
            } catch (Exception e) {
                log.error("", e);
            }
        }
        return list1;
    }

    public static int getCurrentPageFromAntDesignProQueryParam(int currentPage) {
        return currentPage - 1;
    }

    public static Map convertJpaPage2AntDesignTableData(Page page, Class clazz) throws IllegalAccessException, InstantiationException {
        Map pagination = new HashMap();
        pagination.put("total", page.getTotalElements());
        pagination.put("pageSize", page.getSize());
        pagination.put("current", page.getNumber() + 1);

        List list = new Vector();
        for (Object object : page.getContent()) {
            Object targetObject = clazz.newInstance();
            BeanUtils.copyProperties(object, targetObject);
            list.add(targetObject);
        }

        Map dataMap = new HashMap();
        dataMap.put("list", list);
        dataMap.put("pagination", pagination);
        return dataMap;
    }
}
